using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BugSnapper
{
    public partial class LinkDialog : Form
    {
        private bool _accepted = false;
        Logger Logger = new Logger();
        public LinkDialog()
        {
            InitializeComponent();
            LoadUrls();
            linkEdit.TextChanged += new EventHandler(linkEdit_TextChanged);
        }

        void linkEdit_TextChanged(object sender, EventArgs e)
        {
            try
            {
                label1.Text = URL;
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        public string URL
        {
            get
            {
                try
                {
                    return comboBox1.Text + linkEdit.Text.Trim();
                }
                catch (Exception ex)
                {
                    this.Logger.ProcessError(ex);
                    return string.Empty;
                }
            }
        }

        public string URI
        {
            get
            {
                try
                {
                    return linkEdit.Text.Trim();
                }
                catch (Exception ex)
                {
                    this.Logger.ProcessError(ex);
                    return string.Empty;
                }
            }
        }

        public bool Accepted
        {
            get
            {
                try
                {
                    return _accepted;
                }
                catch (Exception ex)
                {
                    this.Logger.ProcessError(ex);
                    return false;
                }
            }
        }

        private void LinkDialog_Load(object sender, EventArgs e)
        {
            try
            {
                label1.Text = URL;
                BeginInvoke((MethodInvoker)delegate
                {
                    linkEdit.Focus();
                });
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void LoadUrls()
        {
            try
            {
                string glob = Properties.Settings.Default.LinkDialogURLs;
                string[] urls = glob.Split(null);
                if (urls != null)
                {
                    foreach (string url in urls)
                    {
                        linkEdit.Items.Add(url);
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            try
            {
                string url = linkEdit.Text;
                string glob = Properties.Settings.Default.LinkDialogURLs;
                if (glob == null) glob = "";
                if (!glob.Contains(url))
                {
                    if (glob.Length > 0)
                        glob += "\n";
                    glob += url;
                }
                Properties.Settings.Default.LinkDialogURLs = glob;
                Properties.Settings.Default.Save();
                _accepted = true;
                Close();
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
                this.Close();
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            try
            {
                _accepted = false;
                Close();
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
                this.Close();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                label1.Text = URL;
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }
    }
}